package com.jianan.mis.app.basedata;

import static com.jianan.mis.util.ui.calendar.SWTCalendarWrapper.openCalendar;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.domain.Country;
import com.jianan.mis.data.domain.School;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.service.CountryService;
import com.jianan.mis.service.SchoolService;
import com.jianan.mis.sys.IGroupKeys;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;
import com.jianan.mis.util.RegularUtils;
import com.jianan.mis.util.StringUtils;

public class SchoolView extends ViewPart {

	private Button btnRemove;
	private StyledText txtSpeciality;
	private Text txtEnterRequirement;
	private Button btnPhoneInterview;
	private Button btnDoubleMatriculate;
	private Text txtCity;
	private Text txtState;
	private Text txtMail;
	private Text txtFax;
	private Text txtPhone;
	private Text txtAddress;
	private Text txtContactName;
	private Text txtWebsite;
	private StyledText txtRemark;
	private Text txtScholarship;
	private Text txtOverseaStudentRate;
	private Text txtStudentCount;
	private Text txtRank;
	private Text txtGraduateRate;
	private Text txtApplyEndDate;
	private Text txtApplyFee;
	private Text txtFee;
	private Text txtLangReq;
	private Combo comboProperty;
	private Combo comboPublic;
	private Text txtCopartner;
	private Combo countryCombo;
	private Text txtName;
	private Text txtSearch;
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	class SchoolListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			return ((School) element).getName();
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	class CountryListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			return ((Country) element).getName();
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	private Combo schoolCombo;
	public static final String ID = "com.jianan.mis.app.basedata.SchoolView"; //$NON-NLS-1$
	
	private ComboViewer schoolComboViewer;
	
	private ComboViewer countryComboViewer;
	
	private String[] properties = new String[] {"语言", "高中", "衔接与预科", "大学", "研究生", "寄宿", "A-Levels", "研究生预科"};

	final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
	
	private CountryService countryService;
	
	private SchoolService schoolService;
	
	private School currentSchool;

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		if(!initializeServices()) {
			MessageDialog.openError(getViewSite().getShell(),
					IMessageKeys.OPEN_VIEW_FAIL, IMessageKeys.SERVICE_UNAVAILABLE);
			return;
		}
		
		final ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
		scrolledComposite.setAlwaysShowScrollBars(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height - 150 > 585 ? 585 : screenSize.height - 150;
		scrolledComposite.setLayoutData(new GridData(SWT.DEFAULT, height));
		
		Composite container = new Composite(scrolledComposite, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.marginTop = 10;
		gridLayout.marginWidth = 0;
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label.setText("选择学校:");

		schoolComboViewer = new ComboViewer(container, SWT.READ_ONLY);
		schoolComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@SuppressWarnings("unchecked")
			public void selectionChanged(final SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				currentSchool = (School) selection.getFirstElement();
				txtName.setText(currentSchool.getName());
				if (currentSchool.getCountry() != null && countryComboViewer.getInput() != null) {
					List<Country> countries = (List<Country>) countryComboViewer.getInput();
					for (int i = 0; i < countries.size(); i++) {
						Country country = countries.get(i);
						if (country.getId().equals(currentSchool.getCountry().getId())) {
							countryCombo.select(i);
						}
					}
				}
				if (currentSchool.getCopartner() != null) {
					txtCopartner.setText(currentSchool.getCopartner());
				} else {
					txtCopartner.setText("");
				}
				if (currentSchool.isPublic()) {
					comboPublic.select(0);
				} else {
					comboPublic.select(1);
				}
				comboProperty.select(ArrayUtils.indexOf(comboProperty.getItems(), currentSchool.getProperty()));
				if (currentSchool.getLangReq() != null) {
					txtLangReq.setText(currentSchool.getLangReq());
				} else {
					txtLangReq.setText("");
				}
				if (currentSchool.getState() != null) {
					txtState.setText(currentSchool.getState());
				} else {
					txtState.setText("");
				}
				if (currentSchool.getCity() != null) {
					txtCity.setText(currentSchool.getCity());
				} else {
					txtCity.setText("");
				}
				btnDoubleMatriculate.setSelection(currentSchool.isDoubleMatriculate());
				btnPhoneInterview.setSelection(currentSchool.isPhoneInterview());
				if (currentSchool.getEnterRequirement() != null) {
					txtEnterRequirement.setText(currentSchool.getEnterRequirement());
				} else {
					txtEnterRequirement.setText("");
				}
				if (currentSchool.getSpeciality() != null) {
					txtSpeciality.setText(currentSchool.getSpeciality());
				} else {
					txtSpeciality.setText("");
				}
				txtFee.setText(currentSchool.getFee().toString());
				if (currentSchool.getApplyFee() != null)
					txtApplyFee.setText(currentSchool.getApplyFee().toString());
				else
					txtApplyFee.setText("");
				if (currentSchool.getApplyEndDate() != null)
					txtApplyEndDate.setText(dateFormat.format(currentSchool.getApplyEndDate()));
				else
					txtApplyEndDate.setText("");
				if (currentSchool.getGraduateRate() != null)
					txtGraduateRate.setText(currentSchool.getGraduateRate().toString());
				else
					txtGraduateRate.setText("");
				if (currentSchool.getRank() != null)
					txtRank.setText(currentSchool.getRank().toString());
				else
					txtRank.setText("");
				if (currentSchool.getStudentCount() != null)
					txtStudentCount.setText(currentSchool.getStudentCount().toString());
				else
					txtStudentCount.setText("");
				if (currentSchool.getOverseaStudentRate() != null)
					txtOverseaStudentRate.setText(currentSchool.getOverseaStudentRate().toString());
				else
					txtOverseaStudentRate.setText("");
				if (currentSchool.getScholarship() != null)
					txtScholarship.setText(currentSchool.getScholarship());
				else
					txtScholarship.setText("");
				if (currentSchool.getRemark() != null)
					txtRemark.setText(currentSchool.getRemark());
				else
					txtRemark.setText("");
				if (currentSchool.getWebsite() != null)
					txtWebsite.setText(currentSchool.getWebsite());
				else
					txtWebsite.setText("");
				txtContactName.setText(currentSchool.getContactName());
				if (currentSchool.getAddress() != null)
					txtAddress.setText(currentSchool.getAddress());
				else
					txtAddress.setText("");
				txtPhone.setText(currentSchool.getPhone());
				txtFax.setText(currentSchool.getFax());
				if (currentSchool.getMail() != null)
					txtMail.setText(currentSchool.getMail());
				else
					txtMail.setText("");
			}
		});
		schoolComboViewer.setLabelProvider(new SchoolListLabelProvider());
		schoolComboViewer.setContentProvider(new ContentProvider());
		schoolCombo = schoolComboViewer.getCombo();
		final GridData gd_schoolCombo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_schoolCombo.widthHint = 100;
		schoolCombo.setLayoutData(gd_schoolCombo);
		schoolComboViewer.setInput(schoolService.getAll());

		txtSearch = new Text(container, SWT.BORDER);
		final GridData gd_txtSearch = new GridData(SWT.RIGHT, SWT.CENTER, true, false);
		gd_txtSearch.widthHint = 120;
		txtSearch.setLayoutData(gd_txtSearch);
		
		final Button btnSearch = new Button(container, SWT.NONE);
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				reset();
				if(StringUtils.isNotEmpty(txtSearch.getText())) {
					List<School> schools = schoolService.findByName(txtSearch.getText().trim());
					schoolComboViewer.setInput(schools);
					if (schools.size() <= 0) {
						MessageDialog.openInformation(e.display.getActiveShell(), 
								IMessageKeys.INFORMATION, "无任何匹配的学校信息!");
					}
				} else {
					schoolComboViewer.setInput(schoolService.getAll());
				}
			}
		});
		btnSearch.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
		btnSearch.setText("搜索学校");

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_2.setText("学校名称:");

		txtName = new Text(container, SWT.BORDER);
		final GridData gd_txtName = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtName.widthHint = 120;
		txtName.setLayoutData(gd_txtName);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_3.setText("国家:");

		countryComboViewer = new ComboViewer(container, SWT.READ_ONLY);
		countryComboViewer.setLabelProvider(new CountryListLabelProvider());
		countryComboViewer.setContentProvider(new ContentProvider());
		countryCombo = countryComboViewer.getCombo();
		final GridData gd_countryCombo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		countryCombo.setLayoutData(gd_countryCombo);
		countryComboViewer.setInput(countryService.getAll());

		final Label label23 = new Label(container, SWT.NONE);
		label23.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label23.setText("合作方:");

		txtCopartner = new Text(container, SWT.BORDER);
		final GridData gd_txtCopartner = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtCopartner.widthHint = 100;
		txtCopartner.setLayoutData(gd_txtCopartner);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_4.setText("公私立:");

		comboPublic = new Combo(container, SWT.READ_ONLY);
		comboPublic.setItems(new String[] {"公立", "私立"});
		final GridData gd_comboPublic = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboPublic.setLayoutData(gd_comboPublic);

		final Label label_5 = new Label(container, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_5.setText("学校性质:");

		comboProperty = new Combo(container, SWT.READ_ONLY);
		comboProperty.setItems(properties);
		final GridData gd_comboProperty = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		comboProperty.setLayoutData(gd_comboProperty);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_1.setText("所在州:");

		txtState = new Text(container, SWT.BORDER);
		final GridData gd_txtState = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtState.widthHint = 120;
		txtState.setLayoutData(gd_txtState);

		final Label label_17 = new Label(container, SWT.NONE);
		label_17.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_17.setText("所在城市:");

		txtCity = new Text(container, SWT.BORDER);
		final GridData gd_txtCity = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtCity.widthHint = 120;
		txtCity.setLayoutData(gd_txtCity);

		final Label label_23 = new Label(container, SWT.NONE);
		label_23.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_23.setText("双录取:");

		btnDoubleMatriculate = new Button(container, SWT.CHECK);

		final Label label_24 = new Label(container, SWT.NONE);
		label_24.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_24.setText("电话面试:");

		btnPhoneInterview = new Button(container, SWT.CHECK);

		final Label label_25 = new Label(container, SWT.NONE);
		label_25.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_25.setText("入学要求:");

		txtEnterRequirement = new Text(container, SWT.BORDER);
		final GridData gd_txtEnterRequirement = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtEnterRequirement.widthHint = 460;
		txtEnterRequirement.setLayoutData(gd_txtEnterRequirement);

		final Label label_26 = new Label(container, SWT.NONE);
		label_26.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		label_26.setText("专业:");

		txtSpeciality = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
		txtSpeciality.setWordWrap(true);
		final GridData gd_txtSpeciality = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtSpeciality.heightHint = 50;
		gd_txtSpeciality.widthHint = 325;
		txtSpeciality.setLayoutData(gd_txtSpeciality);

		final Label label_7 = new Label(container, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_7.setText("学费:");

		txtFee = new Text(container, SWT.BORDER);
		final GridData gd_txtFee = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtFee.widthHint = 100;
		txtFee.setLayoutData(gd_txtFee);

		final Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_6.setText("语言要求:");

		txtLangReq = new Text(container, SWT.BORDER);
		final GridData gd_txtLangReq = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtLangReq.widthHint = 130;
		txtLangReq.setLayoutData(gd_txtLangReq);

		final Label label_8 = new Label(container, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_8.setText("申请费:");

		txtApplyFee = new Text(container, SWT.BORDER);
		final GridData gd_txtApplyFee = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtApplyFee.widthHint = 100;
		txtApplyFee.setLayoutData(gd_txtApplyFee);

		final Label label_9 = new Label(container, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_9.setText("申请截止日期:");

		txtApplyEndDate = new Text(container, SWT.BORDER);
		txtApplyEndDate.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				openCalendar(e, txtApplyEndDate, dateFormat);
			}
		});
		txtApplyEndDate.setEditable(false);
		final GridData gd_txtApplyEndDate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtApplyEndDate.widthHint = 90;
		txtApplyEndDate.setLayoutData(gd_txtApplyEndDate);

		final Label label_10 = new Label(container, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_10.setText("毕业率(%):");

		txtGraduateRate = new Text(container, SWT.BORDER);
		final GridData gd_txtGraduateRate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtGraduateRate.setLayoutData(gd_txtGraduateRate);

		final Label label_11 = new Label(container, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_11.setText("学校排名:");

		txtRank = new Text(container, SWT.BORDER);
		final GridData gd_txtRank = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtRank.setLayoutData(gd_txtRank);

		final Label label_12 = new Label(container, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_12.setText("学生人数:");

		txtStudentCount = new Text(container, SWT.BORDER);
		final GridData gd_txtStudentCount = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtStudentCount.setLayoutData(gd_txtStudentCount);

		final Label label_13 = new Label(container, SWT.NONE);
		label_13.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_13.setText("海外学生比例(%):");

		txtOverseaStudentRate = new Text(container, SWT.BORDER);
		final GridData gd_txtOverseaStudentRate = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		txtOverseaStudentRate.setLayoutData(gd_txtOverseaStudentRate);

		final Label label_14 = new Label(container, SWT.NONE);
		label_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_14.setText("奖学金:");

		txtScholarship = new Text(container, SWT.BORDER);
		final GridData gd_txtScholarship = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtScholarship.widthHint = 210;
		txtScholarship.setLayoutData(gd_txtScholarship);

		final Label label_15 = new Label(container, SWT.NONE);
		label_15.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
		label_15.setText("备注:");

		txtRemark = new StyledText(container, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		txtRemark.setWordWrap(true);
		final GridData gd_txtRemark = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtRemark.heightHint = 50;
		gd_txtRemark.widthHint = 325;
		txtRemark.setLayoutData(gd_txtRemark);

		final Label label_16 = new Label(container, SWT.NONE);
		label_16.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_16.setText("网址:");

		txtWebsite = new Text(container, SWT.BORDER);
		final GridData gd_txtWebsite = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtWebsite.widthHint = 280;
		txtWebsite.setLayoutData(gd_txtWebsite);

		final Label label_18 = new Label(container, SWT.NONE);
		label_18.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_18.setText("联系人姓名:");

		txtContactName = new Text(container, SWT.BORDER);
		final GridData gd_txtContactName = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtContactName.widthHint = 100;
		txtContactName.setLayoutData(gd_txtContactName);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label label_19 = new Label(container, SWT.NONE);
		label_19.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_19.setText("地址:");

		txtAddress = new Text(container, SWT.BORDER);
		final GridData gd_txtAddress = new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1);
		gd_txtAddress.widthHint = 300;
		txtAddress.setLayoutData(gd_txtAddress);

		final Label label_20 = new Label(container, SWT.NONE);
		label_20.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_20.setText("电话号码:");

		txtPhone = new Text(container, SWT.BORDER);
		final GridData gd_txtPhone = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtPhone.widthHint = 115;
		txtPhone.setLayoutData(gd_txtPhone);

		final Label label_21 = new Label(container, SWT.NONE);
		label_21.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_21.setText("传真号码:");

		txtFax = new Text(container, SWT.BORDER);
		final GridData gd_txtFax = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtFax.widthHint = 115;
		txtFax.setLayoutData(gd_txtFax);

		final Label label_22 = new Label(container, SWT.NONE);
		label_22.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		label_22.setText("邮件地址:");

		txtMail = new Text(container, SWT.BORDER);
		final GridData gd_txtMail = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_txtMail.widthHint = 110;
		txtMail.setLayoutData(gd_txtMail);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Button btnSave = new Button(container, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (checkInfo()) {
					School school = new School();
					fillSchoolFields(school);
					try {
						schoolService.save(school);
						reset();
						txtSearch.setText("");
						schoolComboViewer.setInput(schoolService.getAll());
						MessageDialog.openInformation(e.display.getActiveShell(), 
								IMessageKeys.SUCCESS, "新增学校信息成功!");
					} catch(Exception ex) {
						MessageDialog.openError(e.display.getActiveShell(),
								IMessageKeys.FAIL, ex.getMessage());
						return;
					}
				}
			}
		});
		final GridData gd_btnSave = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		btnSave.setLayoutData(gd_btnSave);
		btnSave.setText("添加学校");

		final Button btnUpdate = new Button(container, SWT.NONE);
		btnUpdate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(currentSchool == null) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个学校!");
					return;
				}
				if (checkInfo()) {
					fillSchoolFields(currentSchool);
					try {
						schoolService.update(currentSchool);
						reset();
						txtSearch.setText("");
						schoolComboViewer.setInput(schoolService.getAll());
						MessageDialog.openInformation(e.display.getActiveShell(), 
								IMessageKeys.SUCCESS, "更新学校信息成功!");
					} catch(Exception ex) {
						MessageDialog.openError(e.display.getActiveShell(),
								IMessageKeys.FAIL, ex.getMessage());
						return;
					}
				}
			}
		});
		btnUpdate.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1));
		btnUpdate.setText("更新学校");

		btnRemove = new Button(container, SWT.NONE);
		btnRemove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(currentSchool == null) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个学校!");
					return;
				}
				if(MessageDialog.openConfirm(e.display.getActiveShell(), 
						IMessageKeys.CONFIRM, "确认删除该学校吗?")) {
					try {
						schoolService.remove(currentSchool);
						reset();
						txtSearch.setText("");
						schoolComboViewer.setInput(schoolService.getAll());
						MessageDialog.openInformation(e.display.getActiveShell(), 
								IMessageKeys.SUCCESS, "删除学校信息成功!");
					} catch(Exception ex) {
						MessageDialog.openError(e.display.getActiveShell(),
								IMessageKeys.FAIL, ex.getMessage());
					}
				}
			}
		});
		final GridData gd_btnRemove = new GridData();
		btnRemove.setLayoutData(gd_btnRemove);
		btnRemove.setText("删除学校");
		btnRemove.setEnabled(true);

		container.setSize(container.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setContent(container);
		
		if (Session.getInstance().getGroups().contains(IGroupKeys.MANAGER)) {
			btnRemove.setEnabled(true);
		} else {
			btnRemove.setEnabled(false);
		}
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	private boolean initializeServices() {
		countryService = (CountryService) SpringBeanFactory.getBean("countryService");
		if(countryService == null) {
			return false;
		}
		schoolService = (SchoolService) SpringBeanFactory.getBean("schoolService");
		if(schoolService == null) {
			return false;
		}
		return true;
	}
	
	private void reset() {
		txtName.setText("");
		countryComboViewer.setSelection(null);
		txtCopartner.setText("");
		comboPublic.deselectAll();
		comboProperty.deselectAll();
		txtState.setText("");
		txtCity.setText("");
		btnDoubleMatriculate.setSelection(false);
		btnPhoneInterview.setSelection(false);
		txtEnterRequirement.setText("");
		txtSpeciality.setText("");
		txtLangReq.setText("");
		txtFee.setText("");
		txtApplyFee.setText("");
		txtApplyEndDate.setText("");
		txtGraduateRate.setText("");
		txtRank.setText("");
		txtStudentCount.setText("");
		txtOverseaStudentRate.setText("");
		txtScholarship.setText("");
		txtRemark.setText("");
		txtWebsite.setText("");
		txtContactName.setText("");
		txtAddress.setText("");
		txtPhone.setText("");
		txtFax.setText("");
		txtMail.setText("");
		currentSchool = null;
	}
	
	private void fillSchoolFields(School school) {
		school.setName(txtName.getText());
		if (countryComboViewer.getSelection() != null) {
			IStructuredSelection selection = (IStructuredSelection) countryComboViewer.getSelection();
			Country country = (Country) selection.getFirstElement();
			school.setCountry(country);
		}
		school.setCopartner(txtCopartner.getText());
		if (comboPublic.getSelectionIndex() == 0) {
			school.setPublic(true);
		} else {
			school.setPublic(false);
		}
		school.setProperty(comboProperty.getText());
		if (StringUtils.isNotEmpty(txtState.getText())) {
			school.setState(txtState.getText());
		} else {
			school.setState(null);
		}
		if (StringUtils.isNotEmpty(txtCity.getText())) {
			school.setCity(txtCity.getText());
		} else {
			school.setCity(null);
		}
		school.setDoubleMatriculate(btnDoubleMatriculate.getSelection());
		school.setPhoneInterview(btnPhoneInterview.getSelection());
		if (StringUtils.isNotEmpty(txtEnterRequirement.getText())) {
			school.setEnterRequirement(txtEnterRequirement.getText());
		} else {
			school.setEnterRequirement(null);
		}
		if (StringUtils.isNotEmpty(txtSpeciality.getText())) {
			school.setSpeciality(txtSpeciality.getText());
		} else {
			school.setSpeciality(null);
		}
		school.setFee(txtFee.getText());
		if (StringUtils.isNotEmpty(txtLangReq.getText())) {
			school.setLangReq(txtLangReq.getText());
		} else {
			school.setLangReq(null);
		}
		if (StringUtils.isNotEmpty(txtApplyFee.getText())) {
			school.setApplyFee(txtApplyFee.getText());
		} else {
			school.setApplyFee(null);
		}
		if (StringUtils.isNotEmpty(txtApplyEndDate.getText())) {
			try {
				school.setApplyEndDate(dateFormat.parse(txtApplyEndDate.getText()));
			} catch (ParseException ex) {
				ex.printStackTrace();
			}
		} else {
			school.setApplyEndDate(null);
		}
		if (StringUtils.isNotEmpty(txtGraduateRate.getText())) {
			school.setGraduateRate(new BigDecimal(txtGraduateRate.getText()).setScale(2, BigDecimal.ROUND_HALF_UP));
		} else {
			school.setGraduateRate(null);
		}
		if (StringUtils.isNotEmpty(txtRank.getText())) {
			school.setRank(txtRank.getText());
		} else {
			school.setRank(null);
		}
		if (StringUtils.isNotEmpty(txtStudentCount.getText())) {
			school.setStudentCount(new BigDecimal(txtStudentCount.getText()).setScale(0, BigDecimal.ROUND_HALF_UP));
		} else {
			school.setStudentCount(null);
		}
		if (StringUtils.isNotEmpty(txtOverseaStudentRate.getText())) {
			school.setOverseaStudentRate(new BigDecimal(txtOverseaStudentRate.getText()).setScale(2, BigDecimal.ROUND_HALF_UP));
		} else {
			school.setOverseaStudentRate(null);
		}
		if (StringUtils.isNotEmpty(txtScholarship.getText())) {
			school.setScholarship(txtScholarship.getText());
		} else {
			school.setScholarship(null);
		}
		if (StringUtils.isNotEmpty(txtRemark.getText())) {
			school.setRemark(txtRemark.getText());
		} else {
			school.setRemark(null);
		}
		if (StringUtils.isNotEmpty(txtWebsite.getText())) {
			school.setWebsite(txtWebsite.getText());
		} else {
			school.setWebsite(null);
		}
		school.setContactName(txtContactName.getText());
		if (StringUtils.isNotEmpty(txtAddress.getText())) {
			school.setAddress(txtAddress.getText());
		} else {
			school.setAddress(null);
		}
		school.setPhone(txtPhone.getText());
		school.setFax(txtFax.getText());
		if (StringUtils.isNotEmpty(txtMail.getText())) {
			school.setMail(txtMail.getText());
		} else {
			school.setMail(null);
		}
	}
	
	private boolean checkInfo() {
		if(StringUtils.isEmpty(txtName.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入学校名称!");
			txtName.setFocus();
			return false;
		}
		if(countryCombo.getSelectionIndex() < 0) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请选择国家!");
			countryCombo.setFocus();
			return false;
		}
		if(StringUtils.isEmpty(txtCopartner.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入合作方!");
			txtCopartner.setFocus();
			return false;
		}
		if(comboPublic.getSelectionIndex() < 0) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请选择公私立!");
			comboPublic.setFocus();
			return false;
		}
		if(comboProperty.getSelectionIndex() < 0) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请选择学校性质!");
			comboProperty.setFocus();
			return false;
		}
		if(StringUtils.isEmpty(txtFee.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入学费!");
			txtFee.setFocus();
			return false;
		}
		/*if(!NumberUtils.isNumber(txtFee.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "学费输入错误，请重新输入!");
			txtFee.setFocus();
			return false;
		}
		if(StringUtils.isNotEmpty(txtApplyFee.getText())
				&& !NumberUtils.isNumber(txtApplyFee.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "申请费输入错误，请重新输入!");
			txtApplyFee.setFocus();
			return false;
		}*/
		if(StringUtils.isNotEmpty(txtGraduateRate.getText())
				&& !NumberUtils.isNumber(txtGraduateRate.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "毕业率输入错误，请重新输入!");
			txtGraduateRate.setFocus();
			return false;
		}
		/*if(StringUtils.isNotEmpty(txtRank.getText())
				&& !NumberUtils.isNumber(txtRank.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "学校排名输入错误，请重新输入!");
			txtRank.setFocus();
			return false;
		}*/
		if(StringUtils.isNotEmpty(txtStudentCount.getText())
				&& !NumberUtils.isNumber(txtStudentCount.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "学生人数输入错误，请重新输入!");
			txtStudentCount.setFocus();
			return false;
		}
		if(StringUtils.isNotEmpty(txtOverseaStudentRate.getText())
				&& !NumberUtils.isNumber(txtOverseaStudentRate.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "海外学生比例输入错误，请重新输入!");
			txtOverseaStudentRate.setFocus();
			return false;
		}
		if(StringUtils.isEmpty(txtContactName.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入联系人姓名!");
			txtContactName.setFocus();
			return false;
		}
		if(StringUtils.isEmpty(txtPhone.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入电话号码!");
			txtPhone.setFocus();
			return false;
		}
		if(StringUtils.isEmpty(txtFax.getText())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(), 
					IMessageKeys.INFORMATION, "请输入传真号码!");
			txtFax.setFocus();
			return false;
		}
		if (StringUtils.isNotEmpty(txtMail.getText()) && !RegularUtils.emailCheck(txtMail.getText().trim())) {
			MessageDialog.openError(getViewSite().getWorkbenchWindow().getShell(),
					IMessageKeys.INFORMATION, "E-mail格式输入不正确,请重新输入!");
			txtMail.setFocus();
			return false;
		}
		return true;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
